package cn.theten52.demo.patterns.design.behavior.mediation.update3;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 具体中介者
 *
 * @author wangjin
 * @date 2021/4/4
 */
public class MediatorImpl<T> implements Mediator<T> {
    private Map<Colleague<T>, List<Colleague<T>>> rel = new ConcurrentHashMap<>();

    @Override
    public void change(Colleague<T> colleague) {
        List<Colleague<T>> colleagues = rel.get(colleague);
        if (colleagues != null) {
            colleagues.forEach(c -> c.setData(colleague.getData()));
        }
    }

    @Override
    public void introduce(Colleague<T> provider, Colleague<T> consumer) {
        rel.put(provider, Collections.singletonList(consumer));
    }

    @Override
    public void introduce(Colleague<T> provider, Colleague<T>... consumer) {
        rel.put(provider, Arrays.asList(consumer));
    }
}
